package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.mapper.EmployeeMapper;
import cn.wolfcode.crud.qo.EmployeeQuerObjcet;
import cn.wolfcode.crud.service.IEmployeeService;
import cn.wolfcode.crud.util.NoStringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void delete(Long id) {
        //先把中间表单数据删除
        employeeMapper.deleteER(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Employee employee,Long[] ids) {
        //利用shiro给密码加密,拿到加密后的密码
        Md5Hash hash = new Md5Hash(employee.getPassword(),employee.getName());

        //设置个员工对象
        employee.setPassword(hash.toString());
        //添加员工
        employeeMapper.insert(employee);
        if(ids !=null && ids.length>0) {
            employeeMapper.insertEOID(employee.getId(),ids);
        }

    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);


    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        //先把中间表单数据删除
        employeeMapper.deleteER(employee.getId());
        employeeMapper.updateByPrimaryKey(employee);
        //这个是添加中间表单的
        if(ids !=null && ids.length>0) {
            employeeMapper.insertEOID(employee.getId(),ids);
        }

    }
    //封装页面信息
    @Override
    public PageInfo<Employee> query(EmployeeQuerObjcet eqo) {
        PageHelper.startPage(eqo.getCurrentPage(),eqo.getPageSize());
        List<Employee> formList = employeeMapper.queryFormList(eqo);
        return new PageInfo<Employee>(formList);
    }

    @Override
    public Employee loginSelect(String username, String password) {
        Employee employee=employeeMapper.selectlog(username,password);
        if (employee==null ){
            throw new  RuntimeException("密码就是不对");
        }
        return employee;
    }

    @Override
    public void updatePwd(String newPassword, Long id) {


        employeeMapper.updatePwd(newPassword,id);
    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee checkName(String name) {
        return employeeMapper.checkName(name);
    }

    @Override
    public void saveStatus(long id, boolean status) {
        employeeMapper.saveStatus(id,status);
    }

    @Override
    public Workbook exportXls() {
        //自己创建一个文件
        Workbook wok=new HSSFWorkbook();
        //创建文件名
        Sheet sheet = wok.createSheet("员工信息表格");
        //定义标题行
        Row row = sheet.createRow(0);
        //定义所有标题名称
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("密码");
        row.createCell(2).setCellValue("年龄");
        row.createCell(3).setCellValue("邮箱");
        //拿到所有员工
        List<Employee> employees = employeeMapper.selectAll();
        //历遍拿到是员工,所以要0开始
        for(int i=0;i<employees.size();i++){
            //拿到员工对象
            Employee employee = employees.get(i);
            row = sheet.createRow(i+1);
            //定义所有标题名称
            if (NoStringUtils.noNull(employee.getName())){
                row.createCell(0).setCellValue(employee.getName());
            }
            if (NoStringUtils.noNull(employee.getPassword())){
                row.createCell(1).setCellValue(employee.getPassword());
            }
            if (employee.getAge()!= null){
                row.createCell(2).setCellValue(employee.getAge());
            }
            if (NoStringUtils.noNull(employee.getEmail())){
                row.createCell(3).setCellValue(employee.getEmail());
            }
        }
        return wok;
    }

    @Override
    //导入功能实在是批量添加功能
    public void importXls(MultipartFile file) throws Exception {
        //处理业务把文件放在数据库
        //创建一个文本接收他的流
       Workbook wko = new HSSFWorkbook(file.getInputStream());
        //拿到第一页
        Sheet sheet = wko.getSheetAt(0);
        //拿到这一页中最大的那个行
        int lastRowNum = sheet.getLastRowNum();
        //忽略头开始读
        for (int i=1 ;i<=lastRowNum; i++){
            //n拿到每一行
            Row row = sheet.getRow(i);
            //拿到这行中的没一列数放在对象中
            Employee employee = new Employee();
            //拿第一列
            String name = row.getCell(0).getStringCellValue();
           if(!NoStringUtils.noNull(name)){
               return;
           }
            employee.setName(name);
           //第二列,密码给他一个默认值1
          employee.setPassword("1");
          //第三列
            CellType cellType = row.getCell(2).getCellType();//判断是什么类型,文档还是数
            if (cellType.equals(CellType.STRING)) {
                String age = row.getCell(2).getStringCellValue();
                if(NoStringUtils.noNull(age)){
                    employee.setAge(Integer.valueOf(age));
                }
            }
            if (cellType.equals(CellType.NUMERIC)) {
                double age = row.getCell(2).getNumericCellValue();
                employee.setAge((int)age);
            }
            //第四列e
            String email = row.getCell(3).getStringCellValue();
            employee.setEmail(email);
            //因为密码有加密所有调用原来的方法
            insert(employee,null);
        }
    }

    @Override
    public List<Employee> selectByRole(String... sns) {
        return employeeMapper.selectByRole(sns);
    }


}
